--[[
    api: /device/device_operation
]]

local args = ngx.req.get_uri_args()
local token = args.token
local user_id = nil

if not common_util.check_args(1, token) then
    common_util.exit(200, 0, nil, "101", "token is missing")
end

local user_id = common_util.get_user_id_by_token(token)
if not user_id then
    common_util.exit(200, 0, nil, "105", "token is ....")
end

local handle_band_gateway_quest = function()
    local gateway_id = args.gateway_id
    local gateway_name = args.gateway_name
    if not common_util.check_args(2, gateway_id, gateway_name) then
        common_util.exit(200, 0, nil, "101", "gateway_param is missing")
    end
    --mysql查询语句涉及字符串的一律ngx.quote_sql_str()
    --判断该网关是否已被绑定
    local sql_check = "select status from tb_gateway_info where gateway_id="..ngx.quote_sql_str(gateway_id) .. " and user_id = " .. user_id
    local data,bool = common_util.db_query_common(sql_check,common_config.db_user_info_addr)
    if true ~= bool  then
        --connection:over()
        common_util.exit(200, 0, nil, "500", "mysql is error1")
    end
    if next(data)  then
        if 1 == tonumber(data[1].status) then
            --如果更改gateway_name，则在数据库中更新
            common_util.exit(200, 0, nil, "101", "网关已被绑定")
        elseif 2 == tonumber(data[1].status) then
            local signature = common_util.luuid() --生成网关签名,用于EMQ认证
            -- local sql_replace = "replace into tb_gateway_info set gateway_id="..ngx.quote_sql_str(gateway_id)..",gateway_name="..ngx.quote_sql_str(gateway_name)..
            --      ",user_id="..user_id..",status=2,signature="..ngx.quote_sql_str(signature)
            local sql_replace = "update tb_gateway_info set gateway_name=" .. ngx.quote_sql_str(gateway_name) .. ",signature="..ngx.quote_sql_str(signature) .. " where gateway_id="..ngx.quote_sql_str(gateway_id) .. " and user_id = " .. user_id .. " limit 1"
            local data, bool = common_util.db_query_common(sql_replace,common_config.db_user_info_addr)
            if false == bool then
                common_util.exit(200, 0, nil, "500", "mysql is error1")
            end
            mqtt_util.delete_gateway_cache(gateway_id) --删除网关相关缓存

            mqtt_util.band_device(gateway_id, signature)
            -- TODO,高并发时进入redis缓存处理
            -- mqtt_util.delete_gateway_cache(gateway_id) --删除网关相关缓存
            -- mqtt_util.band_device(gateway_id, signature)
            common_util.exit(200, 1, nil, "200", "band success")
        end
    else
        -- 不存在，插入新的数据
        local signature = common_util.luuid() --生成网关签名,用于EMQ认证
        local sql_insert = "insert into tb_gateway_info set "..
            "gateway_id="..ngx.quote_sql_str(gateway_id)..
            ",gateway_name="..ngx.quote_sql_str(gateway_name)..
            ",user_id="..user_id..",status=2"..
            ",signature="..ngx.quote_sql_str(signature)
        -- sql = "insert into tb_gateway_info set gateway_id="..ngx.quote_sql_str(gateway_id)..",gateway_name="..ngx.quote_sql_str(gateway_name)..",user_id="..user_id..",status=2,signature="..ngx.quote_sql_str(signature)
        local data, bool = common_util.db_query_common(sql_insert,common_config.db_user_info_addr)
        if false == bool  then
            --connection:over()
            common_util.exit(200, 0, nil, "500", "mysql is error2")
        end
        mqtt_util.delete_gateway_cache(gateway_id) --删除网关相关缓存
        mqtt_util.band_device(gateway_id, signature)
        common_util.exit(200, 1, nil, "", "banding")
        -- common_util.exit(200, 0, nil, "500", "data is null")
    end
end

local handle_control_device = function()
    local device_id = args.device_id
    local gateway_id = args.gateway_id
    local control_type = args.control_type
    local control_value = args.control_value

    if not common_util.check_args(4, gateway_id, device_id, control_type, control_value) then
        common_util.exit(200, 0, nil, "101", "param is missing")
    end

    --检查用户操作权限
    local device_info = mqtt_util.get_device_info_by_deviceid_uid(device_id, gateway_id, user_id)
    if not device_info then
        common_util.exit(200, 0, nil, "101", "你没有该设备")
    end

    mqtt_util.control_device(gateway_id, device_id, control_type, control_value)
    common_util.exit(200, 1, nil, "", "success")
end

local handle_set_device = function ()
    
end

local handle_delete_device = function()

end

local method = {
    ["band_gateway_quest"] = handle_band_gateway_quest, --处理绑定网关请求
    ["control_device"] = handle_control_device, --处理控制设备请求
    ["set_device"] = handle_set_device, --设置设备
    ["delete_device"] = handle_delete_device, --设置设备
}

if not method[args.type] then
    common_util.exit(200, 0, nil, "101", "type is missing")
end

method[args.type]() 
common_util.exit(200, 0, nil, "101", "param is error4")


